package com.yayan.beautyclub.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yayan.beautyclub.bean.Ticket;
import com.yayan.beautyclub.service.ITicketService;

@Controller
@RequestMapping(value="/ticket", method=RequestMethod.POST)
public class TicketController {
	
	@Autowired
	private ITicketService ticketService;
	
	/*
	 * 后台管理，分页获取优惠券
	 */
	@RequestMapping(value="/ticket_list.do")
	@ResponseBody
	public List<Ticket> getTicketList(String page, String size){
		try{
			int intPage = Integer.parseInt(page);
			int intSize = Integer.parseInt(size);
			return ticketService.getTicketList(intPage, intSize);
		}catch(Exception e){
			return null;
		}
	}
	
	/*
	 * 后台管理，创建优惠券
	 */
	@RequestMapping(value="/create.do")
	@ResponseBody
	public boolean createTicket(String tName, String startTime, String endTime, String description){				
		try {
			Ticket ticket = new Ticket();
			ticket.settName(tName);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			ticket.setStartTime(df.parse(startTime));
			ticket.setEndTime(df.parse(endTime));			
			ticket.setDescription(description);
			if(ticketService.save(ticket) != 0L)
				return true;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return false;
	}
	
	/*
	 * 后台管理，删除优惠券
	 */
	@RequestMapping(value="/delete_ticket.do")
	@ResponseBody
	public boolean deleteTicket(@RequestBody Map<String, String> map){
		try{
			long tId = Long.parseLong(map.get("tId"));
			return ticketService.deleteTicket(tId);
		}catch(Exception e){
			return false;
		}
	}
	
	@RequestMapping(value="/get_ticket.do")
	@ResponseBody
	public Ticket getTicket(@RequestBody Map<String, String> map){
		long tId = Long.parseLong(map.get("tId"));
		return ticketService.getTicket(tId);
	}	
	
}
